package com.common.utils;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

public class AdDateFormatUtils extends DateFormatUtils {
	
	private static final String[] SERVER_DATE_PATTERN = new String[]{"yyyy-MM-dd HH:mm:ss.SSS"};
	
	private static final String SERVER_DATE_FORMAT_PATTERN = "yyyyMMddHHmmssSSS";
	
	private static final String DAY_FORMAT_PATTERN = "yyyyMMdd";

	/**
	 * 返回格式：yyyyMMddHHmmssSSS
	 * 入参例子：2015-12-10 10:14:15.532
	 * @param udpStr
	 * @return
	 */
	public static final String formatServerTimeStr(String timeStr) {
		Date tmpDate = null;
		try {
			tmpDate = DateUtils.parseDate(timeStr, SERVER_DATE_PATTERN);
		} catch (ParseException e) {
			throw new RuntimeException("exception parse date from=" + timeStr, e);
		}
		String result = DateFormatUtils.format(tmpDate, SERVER_DATE_FORMAT_PATTERN);
		return result;
	}
	
	public static final String formatServerDayTime(String timeStr)
	{
		Date tmpDate = null;
		try {
			tmpDate = DateUtils.parseDate(timeStr, new String[]{SERVER_DATE_FORMAT_PATTERN});
		} catch (ParseException e) {
			throw new RuntimeException("exception parse date from=" + timeStr, e);
		}
		String result = DateFormatUtils.format(tmpDate, DAY_FORMAT_PATTERN);
		return result;
	}
}
